package com.zx._01_基础._019_servlet;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录初始化，如果客户端之前登录过，且session还没有过期，则在打开登录页面时，获取上次的登录信息
 * 登录页面获取登录信息，方便登录
 *
 * @author 张雪
 */
public class HttpSessionServlet2 extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获得session（session对象的创建是在浏览器访问这个站点的时候创建的，这个时候直接获取就可以了）
        Object session = request.getAttribute("mySession");
        //没有获得session
        if (session == null) {
            response.sendRedirect("index.jsp");
            return;
        } else {//获取session，并将需要保存的信息存入
            String[] information = (session + "").split("0.0");
            String name = information[0];
            String password = information[1];
            request.setAttribute("name", name);
            request.setAttribute("password", password);
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
